package com.hrt.datasender.web.protocol;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.hrt.datasender.process.indentity.IMessageIdentity;
import com.hrt.datasender.process.indentity.IMessageIdentityLoader;

@Service
public final class ProtocolService {

    private static volatile ProtocolService instance;

    private ProtocolService() {

    }

    public static ProtocolService getInstance() {
        if (instance == null) {
            synchronized (ProtocolService.class) {
                if (instance == null) {
                    instance = new ProtocolService();
                }
            }
        }
        return instance;
    }

    /**
     * 获取系统中所有存在的协议
     * 
     * @return
     * @throws IOException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws ClassNotFoundException
     */
    public List<MessageIdentityModel> getExistProtocol()
            throws ClassNotFoundException, InstantiationException, IllegalAccessException, IOException {

        List<IMessageIdentity> protocols = IMessageIdentityLoader.getInstance().getExistprotocol();
        List<MessageIdentityModel> protocolModels = new ArrayList<>();

        for (IMessageIdentity protocol : protocols) {
            MessageIdentityModel model = new MessageIdentityModel();
            model.setId(protocol.getProtocolId());
            model.setName(protocol.getProtocolName());

            protocolModels.add(model);
        }

        return protocolModels;
    }

}
